package com.spring.demo.rabbit.config;

import com.spring.demo.rabbit.ExchangeConst;
import com.spring.demo.rabbit.QueueConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/***
 * @title FanoutExchangeConfig
 * @description : 广播  订阅者-发布者
 * @author baiji
 * @version 1.0.0
 * @create 2023/9/4 11:13
 **/
@Configuration
public class FanoutExchangeConfig {

    @Resource
    private AmqpAdmin amqpAdmin;

    /**
     * fanout 交换器 广播
     *
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        FanoutExchange exchange = ExchangeBuilder.fanoutExchange(ExchangeConst.FANOUT_EXCHANGE).build();
        amqpAdmin.declareExchange(exchange);
        Queue fanout_queue_01 = QueueBuilder.durable(QueueConst.FANOUT_QUEUE_01).build();
        amqpAdmin.declareQueue(fanout_queue_01);
        Queue fanout_queue_02 = QueueBuilder.durable(QueueConst.FANOUT_QUEUE_02).build();
        amqpAdmin.declareQueue(fanout_queue_02);
        BindingBuilder.bind(fanout_queue_01).to(exchange);
        BindingBuilder.bind(fanout_queue_02).to(exchange);
        return exchange;
    }
}
